﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasySL.Core;
using EasySL.Data.Interface;
using System.Reflection;
using EasySL.Entity;
using System.Threading;
using EasySL.Share;

namespace EasySL.Service
{
    public partial class DataService
    {
        private readonly string productListCachePath = "/ProductList";

        public List<Product> GetProductList(int page)
        {
            //u can cache the data of productBypage using: 
            //"Cache.Instance.AddItem("/ProductList/Page1", productList)"
            //And when u add a new product, u can clear all cache of productBypage using:
            //Cache.Instance.RemoveItem("/ProductList")"

            if (Cache.Instance.GetItem(productListCachePath) == null)
            {
                Cache.Instance.AddItem(productListCachePath, this.implDAL.GetAllProductList());
            }

            Random rd = new Random();
            Thread.Sleep(rd.Next(0, 3000));

            List<Product> products = Cache.Instance.GetItem(productListCachePath) as List<Product>;
            return products.Skip(15 * (page - 1)).Take(15).ToList();
        }

        public bool DeleteProduct(List<Product> deleteProducts)
        {
            List<Product> products = Cache.Instance.GetItem(productListCachePath) as List<Product>;
            foreach (Product p in deleteProducts)
                products.Remove(p);

            return true;
            
        }

        public List<Category> GetCategoryList()
        {
            return this.implDAL.GetCategoryList();
        }

        public bool UserLogin(string userName, string password)
        {
            return this.implDAL.UserLogin(userName, password);
        }
    }
}
